#ifndef _APP_DATA_H_
#define _APP_DATA_H_
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "bsp_adc.h"

#define Back_left 0x08
#define Turn_left 0x02
#define Change_left 0x04
#define Back_right 19
#define Turn_right 0x03
#define Change_right 0x05
#define Front 0x09

#define APP_TIMEOUT 3000
#define CONFIG_ROT_MAX 1
#define CONFIG_START_SPEED 1

enum nav_cmd_t
{
    NAV_INFO = 0x27,
    NAV_STATUS = 0x35,
    HEART_BEAT = 0x30,
    BRIGHT_MANUAL = 0x3a,
    BRIGHT_AUTO = 0x21,
    UPDATE_FIRMWARE = 0xff
};

enum alarm_area_t
{
    OVER_SPEED,
    HIGH_TEMP,
    LOW_GAS,
    TIRED,
    ALARM_NUM
};

enum nav_dir_t
{
    FRONT = 0x09,
    CHANGE_RIGHT = 0x05,
    CHANGE_LEFT = 0x04,
    TURN_RIGHT = 0x03,
    TURN_LEFT = 0x02,
    BACK_RIGHT = 19,
    BACK_LEFT = 0x08
};

typedef struct nav_info
{
    uint8_t enable;
    uint8_t speed;
    uint8_t speed_limit;
    uint8_t camera;
    uint8_t next_dir;
    uint32_t next_dist;
} nav_info_t;

typedef struct obd_info
{
    uint8_t enable;
    uint8_t speed;
    int16_t water;
    int16_t rotation;        //
    uint8_t rotation_enable; //
    uint8_t gas;
    uint8_t voltage;
    uint32_t last_rotation_enable_time;
    uint32_t last_rotation_disable_time;
    uint32_t last_enable_time;
    uint32_t last_disable_time;
} obd_info_t;

typedef struct alarm
{
    uint8_t alarm;
    uint8_t area[2];
    uint8_t beep_done;
} alarm_t;

typedef struct sensor
{
    float vin;
    float chip_temp;
    float board_temp;
    float light;
    float angle;
} sensor_t;

enum
{
    AUTO = 0,
    MANUAL = 1
};

typedef struct app_data
{
    nav_info_t nav;
    obd_info_t obd;
    sensor_t sensor;
    alarm_t alarm[ALARM_NUM];

    uint8_t sleep_flag;
    uint8_t ble_enable;
    uint8_t can_data[8];
    uint8_t ble_data[50];
    int adc_data[5];
    uint8_t engine_status;
    uint32_t engine_start_time;
    uint32_t engine_stop_time;
    uint16_t standard_angle;

    uint8_t can_type;
    uint32_t zero_degree;

    uint8_t light;
    uint8_t light_mode;
    float backlight_duty;

    uint8_t update_cmd;
    uint8_t phone;
} app_data_t;

extern app_data_t app_data;
extern volatile uint32_t app_tick;
void app_data_task(void *arg);
void app_adc_read_analysis(void);
#endif